﻿using GodSystemCommon;
using GodSystemModel;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GodSystemDAL
{
    public class RolesDAL
    {
        /// <summary>
        /// 根据用户ID，获取用户拥有的角色
        /// </summary>
        /// <param name="users"></param>
        /// <returns></returns>
        public MySqlDataReader GetUserRole(UsersModel users)
        {
            string sql = "select Id,UserId,RoleId from UserRoles where UserId = @UserId";
            MySqlParameter[] pms =
            {
                new MySqlParameter("@UserId",users.Id)
            };
            return SqlHelper.ExecuteReader(sql, pms);
        }
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <returns></returns>
        public MySqlDataReader GetRole()
        {
            string sql = "select Id,RoleName,Remark from Roles";
            return SqlHelper.ExecuteReader(sql);
        }

        /// <summary>
        /// 检测角色名称数量
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public object GetRole(RolesModel role)
        {
            string sql = "select count(1) as total from Roles where RoleName = @RoleName";
            MySqlParameter[] pms =
            {
                new MySqlParameter("@RoleName",role.RoleName)
            };
            return SqlHelper.ExecuteScalar(sql, pms);
        }

        /// <summary>
        /// 增加角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public int AddRole(RolesModel role)
        {
            string sql = "insert into Roles(RoleName) values(@RoleName)";
            MySqlParameter[] pms =
            {
                new MySqlParameter("@RoleName",role.RoleName)
            };
            return SqlHelper.ExecuteNonQuery(sql, pms);
        }
        /// <summary>
        /// 更新角色
        /// </summary>
        /// <param name="role"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public int UpdateRole(RolesModel role, string name)
        {
            string sql = "update Roles set RoleName = @name where RoleName =  @RoleName";
            MySqlParameter[] pms =
            {
                new MySqlParameter("@name",name),
                new MySqlParameter("@RoleName",role.RoleName)
            };
            return SqlHelper.ExecuteNonQuery(sql, pms);
        }
       
    }
}
